package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.Bargain;
import com.bean.Client;
import com.bean.Housepayment;
import com.bean.Indent;
import com.bean.Marketing;
import com.bean.Room;
import com.bean.User;
import com.service.BargainService;
import com.service.ClientService;
import com.service.HousepaymentService;
import com.service.IndentService;
import com.service.MarketingService;
import com.service.RoomService;
import com.service.UserService;
import com.util.Export1;
import com.util.Export2;


@Controller
@RequestMapping("/bargain")
public class bargainaddController{
	
	@Autowired
	private RoomService rs;
	
	@Autowired
	private BargainService bs;
	
	@Autowired
	private HousepaymentService hs;
	
	
	@Autowired
	private IndentService is;
	
	@Autowired
	private MarketingService ms;
	
	
	@Autowired
	private ClientService cs;
	
	
	@Autowired
	private UserService us;
	
	
	@RequestMapping("/bargainlist.do")
	public String bargainlist(HttpServletRequest re,Model model){
		
		Integer uid = (Integer) re.getSession().getAttribute("uid");
		Integer rid = (Integer) re.getSession().getAttribute("rid");
		List<Bargain> list=new ArrayList<Bargain>();
		if(rid==1)
		{
		   list=bs.selectAllByUserid(uid);
		}
		if(rid==2)
		{
			 list=bs.selectAll();
		}
		model.addAttribute("list", list);
		
		return "/bargain/bargainlist";
	}
	
	

	@RequestMapping("/select.do")
	public String select(String cname,String uname,HttpServletRequest re,Model model){
		
		Integer uid = (Integer) re.getSession().getAttribute("uid");
		Integer rid = (Integer) re.getSession().getAttribute("rid");
		List<Bargain> list=new ArrayList<Bargain>();
		if(rid==1)
		{
			
			 Map<String,Object> map=new HashMap<String,Object>();
		    	map.put("cname",cname);
		    	map.put("uname",uname);
		    	map.put("userid",uid);
		   list=bs.selectAllByUseridcnameuname(map);
		}
		if(rid==2)
		{
			 Map<String,Object> map1=new HashMap<String,Object>();
		    	map1.put("cname",cname);
		    	map1.put("uname",uname);
		    	
		   list=bs.selectAllcnameuname(map1);
			
			
		}
		model.addAttribute("list", list);
		
		return "/bargain/bargainlist";
	}
	
	
	
	
	
	@RequestMapping("/daochu.do")
    public void daochu(Integer[] b,HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		List<Bargain> list=new ArrayList<Bargain>();
		
		for(Integer l:b){
			
			Bargain bargain = bs.selectByPrimaryKeyduo(l);
			list.add(bargain);
		}
		System.out.println("size="+list.size());
		response.setHeader("Content-Disposition","attachment;filename=bargain.xls");//指定下载的文件名  
	    response.setContentType("application/vnd.ms-excel");  
		 Export2  ex = new Export2();   
		 ex.buildBaojiaExcel(list, "合同表.xls",response.getOutputStream());
		 
    }
	
	
	
	@RequestMapping("/bargainadd.do")
	public String bargainadd(Integer userid,Integer clientid,Integer roomid,Integer indentid,Integer maketingid,Bargain b){
		System.out.println("aaaa="+b.getSignedtime());
		System.out.println("userid="+userid);
		System.out.println("clientid="+clientid);
		System.out.println("roomid="+roomid);
		System.out.println("maketingid="+maketingid);
		
		//添加签约
		b.setClientid(clientid);
		b.setUserid(userid);
		b.setState("签约");
		b.setMaketingid(maketingid);
		bs.insert(b);
		
		
		//修改房间状态
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id",roomid);
		map.put("state","签约");
		int i = rs.updateByidstate(map);
		System.out.println("修改房间状态="+i);
		
		//修改订单表
		Map<String,Object> map2=new HashMap<String,Object>();
		map2.put("id", indentid);
		map2.put("state","签约");
		int j = is.updateByPrimaryKeystate(map2);
		System.out.println("修改订单表="+j);
		//查找合同编号
		Bargain bargain = bs.selectBymaketingid(maketingid);
		
		//修改机会表
		Map<String,Object> map3=new HashMap<String,Object>();
		map3.put("id", maketingid);
		map3.put("state","签约");
		int l = ms.updateByidstate(map3);
		System.out.println("修改机会表="+l);
		//修改还款信息
		Map<String,Object> map1=new HashMap<String,Object>();
		map1.put("indentid",indentid);
		map1.put("bargainid",bargain.getId());
		int k = hs.updateByindentidbargainId(map1);
		System.out.println("修改还款信息="+k);
		
		return "redirect:/indent/indent.do";
	}
	
	
	
	@RequestMapping("/addbargain.do")
	public String addbargain(Integer ace,Model model){
		Marketing marketing = ms.selectByPrimaryKey(ace);
		
		Integer clientid = marketing.getClientid();
		Client client = cs.selectOne(clientid);
		
		Integer userid = marketing.getUserid();
		User user = us.selectByPrimaryKey(userid);
		Integer roomid = marketing.getRoomid();
		Room room = rs.selectByPrimaryKey(roomid);
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		model.addAttribute("client", client);
		model.addAttribute("user", user);
		model.addAttribute("date",sf.format(date));
		model.addAttribute("room", room);
		model.addAttribute("marketing", marketing.getId());
	
		return "bargain/addbargain";
	}

	
	@RequestMapping("/bargainzadd.do")
	public String bargainzadd(String clientname,String paymenttypeid,Integer userid,Integer clientid,Integer roomid,Integer maketingid,Float dingjin,Bargain b){
		System.out.println("aaaa="+maketingid);
		System.out.println("bbbb="+paymenttypeid);
		//添加合同
		b.setClientid(clientid);
		b.setUserid(userid);
		b.setState("签约");
		bs.insert(b);
		
		
		//修改房间状态
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id",roomid);
		map.put("state","签约");
		int i = rs.updateByidstate(map);
		System.out.println("修改房间状态="+i);
		
		
		//查找合同编号
		Bargain bargain = bs.selectBymaketingid(maketingid);
	    //修改机会状态
		Map<String,Object> map1=new HashMap<String, Object>();
		map1.put("id", maketingid);
		map1.put("state","签约");
		ms.updateByidstate(map1);
		
		//添加还款信息
		System.out.println("aaaacssaaaaaaaaaaaa");
	
		Housepayment ho=new Housepayment();
		ho.setReceivablemoney(b.getRoomtotal());
		ho.setAccomplishmoney(dingjin);
		Float y=(b.getRoomtotal()-dingjin);
		ho.setUncollectedmoney(y);
		ho.setMaketigid(maketingid);
		ho.setPaymenttype(paymenttypeid);
		ho.setFname(clientname);
		System.out.println("aaa="+clientname);
		if(y==0){
			ho.setState("已还完");
		}else{
			ho.setState("未还完");
		}
		ho.setBargainid(bargain.getId());
		ho.setRoomid(roomid);
		ho.setCollectiontime(b.getSignedtime());
		ho.setUserid(userid);
		int j = hs.insert(ho);
		System.out.println("j=="+j);
		
		return "redirect:/marketing/marketing.do";
	}
	
	
	@RequestMapping("/delete.do")
	public String delete(Integer m,Model model){
		
		int i = bs.deleteByPrimaryKey(m);
		
		return "redirect:/bargain/bargainlist.do";
	}
	
	
	@RequestMapping("/toajax90.do")
	public void toajax(Integer m,PrintWriter out,HttpServletRequest re) throws IOException{
	  
		
		
		Housepayment housepayment = hs.selectBybid(m); 
		String state=housepayment.getState();
		
		out.print(state);		
	}
	
	
	
	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}
}
